package com.peg.rules;

import com.peg.dataobjects.Context;

/**
 * @author sad
 */
public class OneOrMore extends ContainerRule {

    private int _lastIterationsCount = 0;

    public OneOrMore(Rule rule) {
        setRule(rule);
    }

    public OneOrMore() {
    }

    @Override
    public boolean execute(Context context) {
        _lastIterationsCount = 0;
        int count = 0;
        while (true) {
            Rule rule=getRule();
            boolean result = executeRule(rule, context);
            if (result) {
                count++;
            } else {
                break;
            }
        }

        _lastIterationsCount = count;
        return count > 0;
    }

    @Override
    public void endRule(boolean result, Context context) {
        if (result) {
            context.setLastIterationsCount(_lastIterationsCount);
        }
    }

    @Override
    public String toString() {
        return "OneOrMore";
    }
}
